home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Microsoft Plateform / Visual Basic 5.0 / Msvb50.ace / msvb50 / MSVB50 / VB / SAMPLES / VISDATA / QUERY.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-07  |  23.0 KB  |  774 lines

  1. VERSION 5.00
  2. Begin VB.Form frmQuery 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "Query Builder"
  5.    ClientHeight    =   5025
  6.    ClientLeft      =   2430
  7.    ClientTop       =   2595
  8.    ClientWidth     =   7455
  9.    BeginProperty Font 
  10.       Name            =   "Tahoma"
  11.       Size            =   8.25
  12.       Charset         =   0
  13.       Weight          =   400
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    HelpContextID   =   2016115
  19.    Icon            =   "QUERY.frx":0000
  20.    KeyPreview      =   -1  'True
  21.    LinkTopic       =   "Form1"
  22.    LockControls    =   -1  'True
  23.    MaxButton       =   0   'False
  24.    MDIChild        =   -1  'True
  25.    ScaleHeight     =   4583.248
  26.    ScaleMode       =   0  'User
  27.    ScaleWidth      =   7358.616
  28.    ShowInTaskbar   =   0   'False
  29.    Begin VB.OptionButton optOrder 
  30.       Caption         =   "Desc"
  31.       Height          =   225
  32.       Index           =   1
  33.       Left            =   6480
  34.       MaskColor       =   &H00000000&
  35.       TabIndex        =   10
  36.       Top             =   1560
  37.       Width           =   855
  38.    End
  39.    Begin VB.OptionButton optOrder 
  40.       Caption         =   "Asc"
  41.       Height          =   221
  42.       Index           =   0
  43.       Left            =   5760
  44.       MaskColor       =   &H00000000&
  45.       TabIndex        =   9
  46.       Top             =   1560
  47.       Value           =   -1  'True
  48.       Width           =   615
  49.    End
  50.    Begin VB.CheckBox chkTopPercent 
  51.       Caption         =   "Top Percent"
  52.       Height          =   255
  53.       Left            =   3840
  54.       MaskColor       =   &H00000000&
  55.       TabIndex        =   15
  56.       Top             =   2880
  57.       Width           =   2175
  58.    End
  59.    Begin VB.TextBox txtTopNValue 
  60.       Height          =   285
  61.       Left            =   3000
  62.       TabIndex        =   14
  63.       Top             =   2880
  64.       Width           =   735
  65.    End
  66.    Begin VB.CommandButton cmdGetValues 
  67.       Caption         =   "List &Possible Values"
  68.       Height          =   315
  69.       Left            =   4560
  70.       MaskColor       =   &H00000000&
  71.       TabIndex        =   5
  72.       Top             =   600
  73.       Width           =   2775
  74.    End
  75.    Begin VB.CommandButton cmdOr 
  76.       Caption         =   "&Or into Criteria"
  77.       Height          =   315
  78.       Left            =   2280
  79.       MaskColor       =   &H00000000&
  80.       TabIndex        =   4
  81.       Top             =   600
  82.       Width           =   2175
  83.    End
  84.    Begin VB.CommandButton cmdAnd 
  85.       Caption         =   "&And into Criteria"
  86.       Height          =   315
  87.       Left            =   120
  88.       MaskColor       =   &H00000000&
  89.       TabIndex        =   3
  90.       Top             =   600
  91.       Width           =   2160
  92.    End
  93.    Begin VB.ComboBox cboValue 
  94.       BackColor       =   &H00FFFFFF&
  95.       Height          =   315
  96.       Left            =   4560
  97.       Sorted          =   -1  'True
  98.       TabIndex        =   2
  99.       Text            =   "cValue"
  100.       Top             =   240
  101.       Width           =   2775
  102.    End
  103.    Begin VB.ComboBox cboOperator 
  104.       BackColor       =   &H00FFFFFF&
  105.       Height          =   315
  106.       ItemData        =   "QUERY.frx":030A
  107.       Left            =   3120
  108.       List            =   "QUERY.frx":030C
  109.       Style           =   2  'Dropdown List
  110.       TabIndex        =   1
  111.       Top             =   240
  112.       Width           =   1335
  113.    End
  114.    Begin VB.ComboBox cboField 
  115.       BackColor       =   &H00FFFFFF&
  116.       Height          =   315
  117.       Left            =   120
  118.       Style           =   2  'Dropdown List
  119.       TabIndex        =   0
  120.       Top             =   240
  121.       Width           =   2895
  122.    End
  123.    Begin VB.CommandButton cmdSaveQDF 
  124.       Caption         =   "Sa&ve"
  125.       Height          =   375
  126.       Left            =   3720
  127.       MaskColor       =   &H00000000&
  128.       TabIndex        =   20
  129.       Top             =   4560
  130.       Width           =   1200
  131.    End
  132.    Begin VB.CommandButton cmdJoin 
  133.       Caption         =   "Set Table &Joins"
  134.       Height          =   255
  135.       Left            =   4560
  136.       MaskColor       =   &H00000000&
  137.       TabIndex        =   12
  138.       Top             =   2160
  139.       Width           =   2775
  140.    End
  141.    Begin VB.ListBox lstJoinFields 
  142.       BackColor       =   &H00FFFFFF&
  143.       Height          =   255
  144.       Left            =   4560
  145.       TabIndex        =   13
  146.       Top             =   2400
  147.       Width           =   2775
  148.    End
  149.    Begin VB.CommandButton cmdCopySQL 
  150.       Caption         =   "Cop&y"
  151.       Height          =   375
  152.       Left            =   2520
  153.       MaskColor       =   &H00000000&
  154.       TabIndex        =   19
  155.       Top             =   4560
  156.       Width           =   1200
  157.    End
  158.    Begin VB.ComboBox cboOrderByField 
  159.       BackColor       =   &H00FFFFFF&
  160.       Height          =   315
  161.       Left            =   4560
  162.       Style           =   2  'Dropdown List
  163.       TabIndex        =   11
  164.       Top             =   1800
  165.       Width           =   2775
  166.    End
  167.    Begin VB.ComboBox cboGroupByField 
  168.       BackColor       =   &H00FFFFFF&
  169.       Height          =   315
  170.       Left            =   4560
  171.       Style           =   2  'Dropdown List
  172.       TabIndex        =   8
  173.       Top             =   1200
  174.       Width           =   2775
  175.    End
  176.    Begin VB.ListBox lstTables 
  177.       BackColor       =   &H00FFFFFF&
  178.       Height          =   1425
  179.       Left            =   120
  180.       MultiSelect     =   1  'Simple
  181.       TabIndex        =   6
  182.       Top             =   1200
  183.       Width           =   1815
  184.    End
  185.    Begin VB.CommandButton cmdShowSQL 
  186.       Caption         =   "&Show"
  187.       Height          =   375
  188.       Left            =   1320
  189.       MaskColor       =   &H00000000&
  190.       TabIndex        =   18
  191.       Top             =   4560
  192.       Width           =   1200
  193.    End
  194.    Begin VB.ListBox lstShowFields 
  195.       BackColor       =   &H00FFFFFF&
  196.       Height          =   1425
  197.       Left            =   2040
  198.       MultiSelect     =   1  'Simple
  199.       TabIndex        =   7
  200.       Top             =   1200
  201.       Width           =   2295
  202.    End
  203.    Begin VB.CommandButton cmdClose 
  204.       Cancel          =   -1  'True
  205.       Caption         =   "&Close"
  206.       Height          =   375
  207.       Left            =   6120
  208.       MaskColor       =   &H00000000&
  209.       TabIndex        =   22
  210.       Top             =   4560
  211.       Width           =   1200
  212.    End
  213.    Begin VB.CommandButton cmdRunQuery 
  214.       Caption         =   "&Run"
  215.       Height          =   375
  216.       Left            =   120
  217.       MaskColor       =   &H00000000&
  218.       TabIndex        =   17
  219.       Top             =   4560
  220.       Width           =   1200
  221.    End
  222.    Begin VB.CommandButton cmdClear 
  223.       Caption         =   "C&lear"
  224.       Height          =   375
  225.       Left            =   4920
  226.       MaskColor       =   &H00000000&
  227.       TabIndex        =   21
  228.       Top             =   4560
  229.       Width           =   1200
  230.    End
  231.    Begin VB.TextBox txtCriteria 
  232.       BackColor       =   &H00FFFFFF&
  233.       Height          =   1215
  234.       Left            =   120
  235.       MultiLine       =   -1  'True
  236.       ScrollBars      =   2  'Vertical
  237.       TabIndex        =   16
  238.       Top             =   3240
  239.       Width           =   7215
  240.    End
  241.    Begin VB.Label lblLabels 
  242.       Caption         =   "Top N Value:"
  243.       Height          =   195
  244.       Index           =   7
  245.       Left            =   1440
  246.       TabIndex        =   31
  247.       Top             =   2910
  248.       Width           =   1470
  249.    End
  250.    Begin VB.Label lblLabels 
  251.       AutoSize        =   -1  'True
  252.       Caption         =   "Operator:"
  253.       Height          =   195
  254.       Index           =   1
  255.       Left            =   3120
  256.       TabIndex        =   30
  257.       Top             =   0
  258.       Width           =   720
  259.    End
  260.    Begin VB.Label lblLabels 
  261.       AutoSize        =   -1  'True
  262.       Caption         =   "Value:"
  263.       Height          =   195
  264.       Index           =   2
  265.       Left            =   4560
  266.       TabIndex        =   29
  267.       Top             =   0
  268.       Width           =   450
  269.    End
  270.    Begin VB.Label lblLabels 
  271.       AutoSize        =   -1  'True
  272.       Caption         =   "Field Name:"
  273.       Height          =   195
  274.       Index           =   0
  275.       Left            =   120
  276.       TabIndex        =   28
  277.       Top             =   0
  278.       Width           =   840
  279.    End
  280.    Begin VB.Label lblLabels 
  281.       AutoSize        =   -1  'True
  282.       Caption         =   "Order By: "
  283.       Height          =   195
  284.       Index           =   6
  285.       Left            =   4560
  286.       TabIndex        =   27
  287.       Top             =   1560
  288.       Width           =   750
  289.    End
  290.    Begin VB.Label lblLabels 
  291.       AutoSize        =   -1  'True
  292.       Caption         =   "Group By: "
  293.       Height          =   195
  294.       Index           =   5
  295.       Left            =   4560
  296.       TabIndex        =   26
  297.       Top             =   960
  298.       Width           =   765
  299.    End
  300.    Begin VB.Label lblLabels 
  301.       AutoSize        =   -1  'True
  302.       Caption         =   "Tables: "
  303.       Height          =   195
  304.       Index           =   3
  305.       Left            =   120
  306.       TabIndex        =   25
  307.       Top             =   960
  308.       Width           =   570
  309.    End
  310.    Begin VB.Label lblLabels 
  311.       AutoSize        =   -1  'True
  312.       Caption         =   "Fields to Show: "
  313.       Height          =   195
  314.       Index           =   4
  315.       Left            =   2040
  316.       TabIndex        =   24
  317.       Top             =   960
  318.       Width           =   1140
  319.    End
  320.    Begin VB.Label lblLabels 
  321.       AutoSize        =   -1  'True
  322.       Caption         =   "Criteria: "
  323.       Height          =   195
  324.       Index           =   8
  325.       Left            =   120
  326.       TabIndex        =   23
  327.       Top             =   3000
  328.       Width           =   630
  329.    End
  330. Attribute VB_Name = "frmQuery"
  331. Attribute VB_GlobalNameSpace = False
  332. Attribute VB_Creatable = False
  333. Attribute VB_PredeclaredId = True
  334. Attribute VB_Exposed = False
  335. Option Explicit
  336. '>>>>>>>>>>>>>>>>>>>>>>>>
  337. Const FORMCAPTION = "Query Builder"
  338. Const BUTTON1 = "&And into Criteria"
  339. Const BUTTON2 = "&Or into Criteria"
  340. Const BUTTON3 = "List &Possible Values"
  341. Const BUTTON4 = "Set Table &Joins"
  342. Const BUTTON5 = "&Run"
  343. Const BUTTON6 = "&Show"
  344. Const BUTTON7 = "Cop&y"
  345. Const BUTTON8 = "Sa&ve"
  346. Const BUTTON9 = "C&lear"
  347. Const BUTTON10 = "&Close"
  348. Const Label1 = "Field Name:"
  349. Const Label2 = "Operator:"
  350. Const LABEL3 = "Value:"
  351. Const LABEL4 = "Tables:"
  352. Const LABEL5 = "Fields to Show:"
  353. Const LABEL6 = "Group By:"
  354. Const LABEL7 = "Order By:"
  355. Const LABEL8 = "Top N Value:"
  356. Const LABEL9 = "Criteria:"
  357. Const CHECK1 = "Top Percent"
  358. Const MSG1 = "Updating Form Fields"
  359. Const MSG2 = "(none)"
  360. Const MSG3 = "You Must Have at Least 2 Tables Selected!"
  361. Const MSG4 = "Choose Joins"
  362. Const MSG5 = "No Query Entered!"
  363. Const MSG6 = "Building Query"
  364. Const MSG7 = "Running Query"
  365. Const MSG8 = "Enter QueryDef Name:"
  366. '>>>>>>>>>>>>>>>>>>>>>>>>
  367. Dim mbShowSQL As Integer
  368. Dim mbCopySQL As Integer
  369. Dim mbSaveSQL As Integer
  370. Private Sub cmdAnd_Click()
  371.   Dim nFldType As Integer
  372.   Dim sFieldName As String
  373.   Dim sTableName As String
  374.   If Len(cboField.Text) = 0 Then Exit Sub
  375.   sTableName = stSTF((cboField), 0)
  376.   sFieldName = stSTF((cboField), 1)
  377.   nFldType = gdbCurrentDB.TableDefs(StripBrackets(sTableName)).Fields(StripBrackets(sFieldName)).Type
  378.   If Len(txtCriteria.Text) > 0 Then
  379.     txtCriteria.Text = txtCriteria.Text & vbCrLf & "And "
  380.   End If
  381.   If nFldType = dbText Or nFldType = dbMemo Or nFldType = dbDate Then
  382.     txtCriteria.Text = txtCriteria.Text & cboField.Text & " " & cboOperator.Text & " '" & cboValue.Text & "'"
  383.   Else
  384.     txtCriteria.Text = txtCriteria.Text & cboField.Text & " " & cboOperator.Text & " " & cboValue.Text
  385.   End If
  386.   cboField.SetFocus
  387. End Sub
  388. Private Sub cboField_Click()
  389.   cboValue.Clear
  390. End Sub
  391. Private Sub cmdClear_Click()
  392.   On Error Resume Next
  393.   Dim i As Integer
  394.   For i = 0 To lstTables.ListCount - 1
  395.     lstTables.Selected(i) = False
  396.   Next
  397.   txtCriteria.Text = vbNullString
  398.   txtTopNValue.Text = vbNullString
  399. End Sub
  400. Private Sub cmdClose_Click()
  401.   Unload Me
  402. End Sub
  403. Private Sub cmdCopySQL_Click()
  404.   mbCopySQL = True
  405.   Call cmdRunQuery_Click
  406.   mbCopySQL = False
  407. End Sub
  408. Private Sub cmdSaveQDF_Click()
  409.   mbSaveSQL = True
  410.   Call cmdRunQuery_Click
  411.   mbSaveSQL = False
  412. End Sub
  413. Private Sub lstTables_Click()
  414.   On Error GoTo LTErr
  415.   Dim i As Integer, ii As Integer
  416.   Dim tdf As TableDef
  417.   Dim qdf As QueryDef
  418.   Dim sTmp As String
  419.   Dim fld As Field
  420.   MsgBar MSG1, True
  421.   cboField.Clear
  422.   lstShowFields.Clear
  423.   cboGroupByField.Clear
  424.   cboOrderByField.Clear
  425.   cboValue.Clear
  426.   cboGroupByField.AddItem MSG2
  427.   cboOrderByField.AddItem MSG2
  428.   For ii = 0 To lstTables.ListCount - 1
  429.     If lstTables.Selected(ii) Then
  430.       If lstTables.ItemData(ii) = 0 Then
  431.         'must be a table
  432.         Set tdf = gdbCurrentDB.TableDefs(lstTables.List(ii))
  433.         For Each fld In tdf.Fields
  434.           sTmp = AddBrackets((lstTables.List(ii))) & "." & AddBrackets((fld.Name))
  435.           cboField.AddItem sTmp
  436.           lstShowFields.AddItem sTmp
  437.           cboGroupByField.AddItem sTmp
  438.           cboOrderByField.AddItem sTmp
  439.         Next
  440.       Else
  441.         'must be a querydef
  442.         Set qdf = gdbCurrentDB.QueryDefs(lstTables.List(ii))
  443.         For Each fld In qdf.Fields
  444.           sTmp = AddBrackets((lstTables.List(ii))) & "." & AddBrackets((fld.Name))
  445.           cboField.AddItem sTmp
  446.           lstShowFields.AddItem sTmp
  447.           cboGroupByField.AddItem sTmp
  448.           cboOrderByField.AddItem sTmp
  449.         Next
  450.       End If
  451.     End If
  452.   Next
  453.   If Len(cboField.List(0)) > 0 Then
  454.     cboField.ListIndex = 0
  455.     cboGroupByField.ListIndex = 0
  456.     cboOrderByField.ListIndex = 0
  457.   End If
  458.   MsgBar vbNullString, False
  459.   Exit Sub
  460. LTErr:
  461.   ShowError
  462. End Sub
  463. Private Sub Form_Load()
  464.   On Local Error GoTo FLErr
  465.   Dim rec As Recordset
  466.   Dim i As Integer
  467.   Me.Caption = FORMCAPTION
  468.   cmdAnd.Caption = BUTTON1
  469.   cmdOr.Caption = BUTTON2
  470.   cmdGetValues.Caption = BUTTON3
  471.   cmdJoin.Caption = BUTTON4
  472.   cmdRunQuery.Caption = BUTTON5
  473.   cmdShowSQL.Caption = BUTTON6
  474.   cmdCopySQL.Caption = BUTTON7
  475.   cmdSaveQDF.Caption = BUTTON8
  476.   cmdClear.Caption = BUTTON9
  477.   cmdClose.Caption = BUTTON10
  478.   lblLabels(0).Caption = Label1
  479.   lblLabels(1).Caption = Label2
  480.   lblLabels(2).Caption = LABEL3
  481.   lblLabels(3).Caption = LABEL4
  482.   lblLabels(4).Caption = LABEL5
  483.   lblLabels(5).Caption = LABEL6
  484.   lblLabels(6).Caption = LABEL7
  485.   lblLabels(7).Caption = LABEL8
  486.   lblLabels(8).Caption = LABEL9
  487.   chkTopPercent.Caption = CHECK1
  488.   'Clear listbox
  489.   txtCriteria.Text = vbNullString
  490.   cboOperator.AddItem "="
  491.   cboOperator.AddItem "<>"
  492.   cboOperator.AddItem ">"
  493.   cboOperator.AddItem ">="
  494.   cboOperator.AddItem "<"
  495.   cboOperator.AddItem "<="
  496.   cboOperator.AddItem "Like"
  497.   cboOperator.ListIndex = 0
  498.   'fill the table list
  499.   GetTableList lstTables, False, False, True
  500.   lstTables.ListIndex = 0
  501.   cboValue.Text = vbNullString
  502.   Height = 5520
  503.   Width = 7224
  504.   Left = (frmMDI.Width - Width) / 2
  505.   Top = 0
  506.   Exit Sub
  507. FLErr:
  508.   ShowError
  509. End Sub
  510. Private Sub Form_Resize()
  511.   On Error Resume Next
  512.   If WindowState <> 1 Then
  513.     Me.Height = 5430
  514.     Me.Width = 7575
  515.   End If
  516. End Sub
  517. Private Sub cmdGetValues_Click()
  518.   On Error GoTo GVErr
  519.   Dim rec As Recordset
  520.   MsgBar "Getting Possible Values", True
  521.   Screen.MousePointer = vbHourglass
  522.   Set rec = gdbCurrentDB.OpenRecordset("select Distinct " & cboField & " from " & stSTF((cboField), 0))
  523.   Do While rec.EOF = False
  524.     If Len(Trim(rec(0))) > 0 Then
  525.       cboValue.AddItem rec(0).Value
  526.     End If
  527.     rec.MoveNext
  528.   Loop
  529.   rec.Close
  530.   cboValue.Text = cboValue.List(0)
  531.   cboValue.SetFocus
  532.   Screen.MousePointer = vbDefault
  533.   MsgBar vbNullString, False
  534.   Exit Sub
  535. GVErr:
  536.   Screen.MousePointer = vbDefault
  537.   MsgBar vbNullString, False
  538.   cboValue.Text = vbNullString
  539.   Exit Sub
  540. End Sub
  541. Private Sub cmdJoin_Click()
  542.   Dim i As Integer
  543.   Dim c As Integer
  544.   For i = 0 To lstTables.ListCount - 1
  545.     If lstTables.Selected(i) Then
  546.       c = c + 1
  547.     End If
  548.   Next
  549.   If c < 2 Then
  550.     Beep
  551.     MsgBox MSG3, 48
  552.   Else
  553.     MsgBar MSG4, False
  554.     frmJoin.Show vbModal
  555.     MsgBar vbNullString, False
  556.   End If
  557. End Sub
  558. Private Sub cmdOr_Click()
  559.   Dim nType As Integer
  560.   Dim sFieldName As String
  561.   Dim sTableName As String
  562.   If Len(cboField.Text) = 0 Then Exit Sub
  563.   sTableName = stSTF((cboField), 0)
  564.   sFieldName = stSTF((cboField), 1)
  565.   nType = gdbCurrentDB.TableDefs(StripBrackets(sTableName)).Fields(StripBrackets(sFieldName)).Type
  566.   If Len(txtCriteria.Text) > 0 Then
  567.     txtCriteria.Text = txtCriteria.Text & vbCrLf & " Or "
  568.   End If
  569.   If nType = dbText Or nType = dbMemo Or nType = dbDate Then
  570.     txtCriteria.Text = txtCriteria.Text & cboField.Text & " " & cboOperator.Text & " '" & cboValue.Text & "'"
  571.   Else
  572.     txtCriteria.Text = txtCriteria.Text & cboField.Text & " " & cboOperator.Text & " " & cboValue.Text
  573.   End If
  574.   cboField.SetFocus
  575. End Sub
  576. Private Sub cmdRunQuery_Click()
  577.   On Error GoTo OKErr
  578.   Dim rsTmp As Recordset
  579.   Dim frmTmp As Form
  580.   Dim fs As String
  581.   Dim ts As String
  582.   Dim i As Integer
  583.   Dim sWhere As String
  584.   Dim sWhere2 As String
  585.   Dim sNewWhere As String
  586.   Dim sTmp As String
  587.   Dim bMatchParen As Integer
  588.   Dim sQueryName As String
  589.   Dim qdfTmp As QueryDef
  590.   Dim sSQLString As String
  591.   If lstShowFields.ListCount = 0 Then
  592.     MsgBox MSG5, vbExclamation
  593.     Exit Sub
  594.   End If
  595.   MsgBar MSG6, True
  596.   If Len(txtCriteria.Text) > 0 Then
  597.     sWhere = "AND " & LTrim(txtCriteria.Text)
  598.     'strip vbcrlfs
  599.     For i = 1 To Len(sWhere)
  600.       If Mid(sWhere, i, 1) = Chr(13) Then
  601.         sTmp = sTmp & " "
  602.       ElseIf Mid(sWhere, i, 1) = Chr(10) Then
  603.         'do nothing
  604.       Else
  605.         sTmp = sTmp + Mid(sWhere, i, 1)
  606.       End If
  607.     Next
  608.     sWhere = sTmp
  609.     sWhere = RTrim(sWhere)
  610.     'Add parens to sWhere
  611.      sWhere2 = sWhere
  612.      Do
  613.        sTmp = stGetToken(sWhere2, " ")
  614.        sTmp = sTmp & " "
  615.         If bMatchParen = False And UCase(sTmp) = "AND " Then
  616.          sNewWhere = sNewWhere + sTmp & "("
  617.          bMatchParen = True
  618.        ElseIf bMatchParen And UCase(sTmp) = "AND " Then
  619.          sNewWhere = sNewWhere & ") " & sTmp & "("
  620.          'bMatchParen = False
  621.        Else
  622.          If UCase(sTmp) = "OR" Or UCase(sTmp) = "IN " Or UCase(sTmp) = "LIKE" Then
  623.            sNewWhere = sNewWhere & " " & sTmp
  624.          Else
  625.            sNewWhere = sNewWhere + sTmp
  626.          End If
  627.        End If
  628.      Loop Until sWhere2 = vbNullString
  629.      sWhere = sNewWhere & ")"
  630.     'Build DynaSet string:
  631.     'Peel off leading AND/OR
  632.     If Mid(sWhere, 2, 2) = "OR" Then
  633.       sWhere = Mid(sWhere, 5, Len(sWhere) - 5)
  634.     Else
  635.       sTmp = stGetToken(sWhere, " ")
  636.     End If
  637.     If Len(sWhere) > 0 Then
  638.       sWhere = " Where " & sWhere
  639.     End If
  640.   End If
  641.   'check for join condition
  642.   If lstJoinFields.ListCount > 0 Then
  643.     If Len(sWhere) = 0 Then
  644.       sWhere = sWhere & " Where "
  645.     Else
  646.       sWhere = sWhere & " And "
  647.     End If
  648.     For i = 0 To lstJoinFields.ListCount - 1
  649.       sWhere = sWhere + lstJoinFields.List(i) & " And "
  650.     Next
  651.     sWhere = Mid(sWhere, 1, Len(sWhere) - 5)
  652.   End If
  653.   'check for group by field
  654.   If cboGroupByField <> MSG2 Then
  655.     sWhere = sWhere & " Group By " & cboGroupByField
  656.   End If
  657.   'check for order by field
  658.   If cboOrderByField <> MSG2 Then
  659.     sWhere = sWhere & " Order By " & cboOrderByField
  660.     If optOrder(1).Value Then
  661.       sWhere = sWhere & " Desc "
  662.     End If
  663.   End If
  664.   'get show field names
  665.   For i% = 0 To lstShowFields.ListCount - 1
  666.     If lstShowFields.Selected(i%) Then
  667.       fs = fs + lstShowFields.List(i%) & ","
  668.     End If
  669.   Next
  670.   If Len(fs) = 0 Then
  671.     For i% = 0 To lstTables.ListCount - 1
  672.       If lstTables.Selected(i%) Then
  673.         fs = fs + AddBrackets((lstTables.List(i%))) & ".*,"
  674.       End If
  675.     Next
  676.     If Len(fs) = 0 Then
  677.       fs = "*"
  678.     Else
  679.       fs = Mid(fs, 1, Len(fs) - 1)     'take off the last ","
  680.     End If
  681.   Else
  682.     fs = Mid(fs, 1, Len(fs) - 1)
  683.   End If
  684.   'get table names
  685.   For i% = 0 To lstTables.ListCount - 1
  686.     If lstTables.Selected(i%) Then
  687.       ts = ts + AddBrackets((lstTables.List(i%))) & ","
  688.     End If
  689.   Next
  690.   ts = Mid(ts, 1, Len(ts) - 1)
  691.   sSQLString = "Select "
  692.   'set Top N Value if present
  693.   If Len(txtTopNValue.Text) > 0 Then
  694.     sSQLString = sSQLString & " TOP " & txtTopNValue.Text & " "
  695.     If chkTopPercent.Value = vbChecked Then
  696.       sSQLString = sSQLString & " PERCENT "
  697.     End If
  698.   End If
  699.   sSQLString = sSQLString & fs & " From " & ts + sWhere
  700.   If mbShowSQL = False And mbCopySQL = False And mbSaveSQL = False Then
  701.     MsgBar MSG7, True
  702.     OpenQuery sSQLString, True
  703.   ElseIf mbShowSQL Then
  704.     MsgBar vbNullString, False
  705.     MsgBox sSQLString, 0, "SQL Query"
  706.   ElseIf mbCopySQL Then
  707.     frmSQL.txtSQLStatement.Text = sSQLString
  708.   ElseIf mbSaveSQL Then
  709.     MsgBar vbNullString, False
  710.     sQueryName = InputBox(MSG8)
  711.     If Len(sQueryName) = 0 Then Exit Sub
  712.     'check for a dupe and exit if the user won't overwrite it
  713.     If DupeTableName(sQueryName) Then
  714.       Exit Sub
  715.     End If
  716.     'add the new querydef
  717.     Set qdfTmp = gdbCurrentDB.CreateQueryDef(sQueryName, sSQLString)
  718.     RefreshTables Nothing
  719.   End If
  720.   MsgBar vbNullString, False
  721.   Exit Sub
  722. OKErr:
  723.   If Err = 364 Then Exit Sub   'catch unloaded form
  724.   ShowError
  725. End Sub
  726. Private Sub cmdShowSQL_Click()
  727.   mbShowSQL = True
  728.   Call cmdRunQuery_Click
  729.   mbShowSQL = False
  730. End Sub
  731. Private Function stGetToken(rsLine As String, rsDelim As String) As String
  732.   On Error GoTo GetTokenError
  733.   Dim iOpenQuote As Integer
  734.   Dim iCloseQuote As Integer
  735.   Dim iDelim As Integer
  736.   Dim stToken As String
  737.   iOpenQuote = InStr(1, rsLine, """")
  738.   iDelim = InStr(1, rsLine, rsDelim)
  739.   If (iOpenQuote > 0) And (iOpenQuote < iDelim) Then
  740.     iCloseQuote = InStr(iOpenQuote + 1, rsLine, """")
  741.     iDelim = InStr(iCloseQuote + 1, rsLine, rsDelim)
  742.   End If
  743.   If (iDelim% <> 0) Then
  744.     stToken = LTrim(RTrim(Mid(rsLine, 1, iDelim - 1)))
  745.     rsLine = Mid(rsLine, iDelim + 1)
  746.   Else
  747.     stToken = LTrim(RTrim(Mid(rsLine, 1)))
  748.     rsLine = vbNullString
  749.   End If
  750.   If (Len(stToken) > 0) Then
  751.     If (Mid(stToken, 1, 1) = """") Then
  752.       stToken = Mid(stToken, 2)
  753.     End If
  754.     If (Mid(stToken, Len(stToken), 1) = """") Then
  755.       stToken = Mid(stToken, 1, Len(stToken) - 1)
  756.     End If
  757.   End If
  758.   stGetToken = stToken
  759.   Exit Function
  760. GetTokenError:
  761.   Exit Function
  762. End Function
  763. 'function to split the table and the field from a tbl.fld pair
  764. Private Function stSTF(rsName As String, rnPart As Integer) As String
  765.   If InStr(InStr(1, rsName, ".") + 1, rsName, ".") > 1 Then
  766.     rsName = StripOwner(rsName)
  767.   End If
  768.   If rnPart = 0 Then
  769.     stSTF = Mid(rsName, 1, InStr(1, rsName, ".") - 1)
  770.   Else
  771.     stSTF = Mid(rsName, InStr(1, rsName, ".") + 1, Len(rsName))
  772.   End If
  773. End Function
  774.